package org.example.com.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.com.Bean.User;
import org.example.com.Util.JsonResult;
import org.example.com.Util.ResultTool;
import org.example.com.config.JwtConfig;
import org.example.com.mapper.UserMapper;
import org.example.com.service.UserService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper mapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public JsonResult login(User user) {
        User result= mapper.login(user);
        if(result==null){
            return ResultTool.fail(501,"账号或者密码错误");
        }
        String token = JwtConfig.getJwtToken(result);
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("token:"+result.getId(),token);
        return ResultTool.success(token);
    }
}
